package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import pojo.Room;
import service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/room")
@Api(tags = "诊室管理API", description = "诊室信息的增删改查及查询接口")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @PostMapping("/add")
    @ApiOperation(value = "添加诊室信息", notes = "传入诊室完整信息（roomId无需填写，自动生成）")
    public Map<String, Object> addRoom(@RequestBody @ApiParam(name = "room", value = "诊室实体对象", required = true) Room room) {
        Map<String, Object> result = new HashMap<>();
        int count = roomService.addRoom(room);
        result.put("success", count > 0);
        result.put("message", count > 0 ? "添加成功" : "添加失败");
        return result;
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新诊室信息", notes = "根据roomId更新诊室信息，需传入完整对象")
    public Map<String, Object> updateRoom(@RequestBody @ApiParam(name = "room", value = "诊室实体对象", required = true) Room room) {
        Map<String, Object> result = new HashMap<>();
        int count = roomService.updateRoom(room);
        result.put("success", count > 0);
        result.put("message", count > 0 ? "更新成功" : "更新失败");
        return result;
    }

    @DeleteMapping("/delete/{roomId}")
    @ApiOperation(value = "删除诊室信息", notes = "根据roomId删除诊室，逻辑删除或物理删除取决于服务实现")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "诊室ID", paramType = "path", dataType = "Integer", required = true, example = "1")
    })
    public Map<String, Object> deleteRoom(@PathVariable Integer roomId) {
        Map<String, Object> result = new HashMap<>();
        int count = roomService.deleteRoom(roomId);
        result.put("success", count > 0);
        result.put("message", count > 0 ? "删除成功" : "删除失败");
        return result;
    }

    @GetMapping("/{roomId}")
    @ApiOperation(value = "根据ID查询诊室详情", notes = "返回指定roomId的诊室信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "诊室ID", paramType = "path", dataType = "Integer", required = true, example = "1")
    })
    public Room getRoom(@PathVariable Integer roomId) {
        return roomService.getRoomById(roomId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有诊室列表", notes = "返回所有诊室的简要信息列表")
    public List<Room> listRooms() {
        return roomService.getAllRooms();
    }

    @GetMapping("/list/floor/{floor}")
    @ApiOperation(value = "根据楼层查询诊室", notes = "返回指定楼层的所有诊室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "floor", value = "楼层号", paramType = "path", dataType = "Integer", required = true, example = "3")
    })
    public List<Room> listRoomsByFloor(@PathVariable Integer floor) {
        return roomService.getRoomsByFloor(floor);
    }

    @GetMapping("/list/depart/{departId1}")
    @ApiOperation(value = "根据科室ID查询诊室", notes = "返回指定科室ID的所有诊室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departId1", value = "科室ID", paramType = "path", dataType = "Integer", required = true, example = "5")
    })
    public List<Room> listRoomsByDepartId1(@PathVariable Integer departId1) {
        return roomService.getRoomsByDepartId1(departId1);
    }
}